﻿using FaceCat;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace facecat_devexpress
{
    /// <summary>
    /// 主窗体
    /// </summary>
    public partial class Window : DevExpress.XtraEditors.XtraForm
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="xml">配置解析</param>
        /// <param name="path">路径</param>
        public Window(FCUIXml xml, String path)
        {
            InitializeComponent();
            m_xml = xml;
            m_xml.createNative();
            m_native = m_xml.getNative();
            m_native.setPaint(new GdiPlusPaint());
            m_host = new WinHost();
            m_host.setNative(m_native);
            m_native.setHost(m_host);
            m_host.setHWnd(Handle);
            FCSize clientSize = new FCSize(ClientSize.Width, ClientSize.Height);
            m_native.setSize(new FCSize(clientSize.cx, clientSize.cy));
            m_native.setScaleSize(new FCSize((int)(clientSize.cx * m_scaleFactor), (int)(clientSize.cy * m_scaleFactor)));
            m_native.setAllowScaleSize(true);
            m_xml.loadFile(path, null);
            m_native.update();
            Invalidate();
        }

        /// <summary>
        /// 公共方法库
        /// </summary>
        public FCNative m_native;

        /// <summary>
        /// 设备对接
        /// </summary>
        public WinHost m_host;

        /// <summary>
        /// 配置解析
        /// </summary>
        private FCUIXml m_xml;
        
        /// <summary>
        /// 缩放比例
        /// </summary>
        public double m_scaleFactor = 0.5;

        /// <summary>
        /// 尺寸改变方法
        /// </summary>
        /// <param name="e">参数</param>
        protected override void OnSizeChanged(EventArgs e)
        {
            base.OnSizeChanged(e);
            if (m_native != null)
            {
                FCSize clientSize = new FCSize(ClientSize.Width, ClientSize.Height);
                m_native.setSize(clientSize);
                m_native.setScaleSize(new FCSize((int)(clientSize.cx * m_scaleFactor), (int)(clientSize.cy * m_scaleFactor)));
                m_native.update();
                m_native.invalidate();
            }
        }

        /// <summary>
        /// 消息监听
        /// </summary>
        /// <param name="m"></param>
        protected override void WndProc(ref Message m)
        {
            try
            {
                if (m_host != null)
                {
                    if (m_host.onMessage(ref m) > 0)
                    {
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
            }
            base.WndProc(ref m);
        }
    }
}
